const Redis = require('redis');

const redisClient = Redis.createClient({
    url: process.env.REDIS_URL || 'redis://localhost:6379',
    socket: {
        reconnectStrategy: (retries) => {
            // 重连策略
            if (retries > 10) {
                return new Error('Redis连接重试次数过多。');
            }
            return Math.min(retries * 100, 3000);
        }
    }
});

redisClient.on('error', (err) => {
    console.error('Redis 错误:', err);
});

redisClient.on('connect', () => {
    console.log('Redis 连接成功');
});

const connectRedis = async () => {
    try {
        await redisClient.connect();
    } catch (err) {
        console.error('Redis 连接失败:', err);
    }
};

module.exports = {
    redisClient,
    connectRedis
}; 